package com.dycx.cms.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.cms.dto.KeywordDTO;
import com.dycx.cms.entity.KeywordDO;
import com.dycx.cms.mapper.KeywordMapper;
import com.dycx.cms.service.KeywordService;
import com.dycx.cms.vo.KeywordVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 关键字(Keyword)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-05-24 11:46:35
 */

@Service
@AllArgsConstructor
public class KeywordServiceImpl extends ServiceImpl<KeywordMapper, KeywordDO> implements KeywordService {

    private KeywordMapper keywordMapper;


    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    public Integer add(KeywordDTO dto) {
        KeywordDO entity = BeanUtil.toBean(dto, KeywordDO.class);

        if (this.save(entity)) {
            return entity.getId();
        }

        return 0;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean edit(KeywordDTO dto) {
        return this.updateById(BeanUtil.toBean(dto, KeywordDO.class));
    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public KeywordVO getInfo(Integer id) {
        KeywordDO entity = this.getById(id);
        return BeanUtil.toBean(entity, KeywordVO.class);
    }

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<KeywordVO> getPage(KeywordDTO dto) {
        Page<KeywordVO> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        return this.keywordMapper.getPage(page, dto);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Boolean delete(Integer id) {
        return this.removeById(id);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return this.keywordMapper.getMaxSort();
    }
}

